package com.yc.UI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.dao.UserDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.Values;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;


/**
 *充值界面
 * @author UNO
 * 2021年6月28日下午4:07:43
 */
public class AddBalance {

	protected Shell shell;
	private boolean down = false;
	private int clickX = 0;
	private int clickY = 0;
	private Text text_money;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			AddBalance window = new AddBalance();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.NONE);
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/福字.png"));	
		shell.setSize(905, 507);
		shell.setText("");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		
		//背景图片
		composite.setBackgroundImage(SWTResourceManager.getImage(WelcomeUI.class, "/images/back.jpg"));
		// 设置窗口居中
		SWTUtil.centerWindom(shell);

		
		//设置图标
		Label label_Icon = new Label(composite, SWT.NONE);
		label_Icon.setBounds(0, 0, 40, 40);
		label_Icon.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/福字.png"));
		
		//关闭按钮
		Label label_close = new Label(composite, SWT.NONE);
		
		label_close.setBounds(863, 0, 40, 40);
		label_close.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/关闭.png"));
		
		//最小化按钮
		Label label_min = new Label(composite, SWT.NONE);
		label_min.setBounds(817, 0, 40, 40);
		label_min.setImage(SWTResourceManager.getImage(WelcomeUI.class,"/images/最小化.png"));
		
		Label Label_title = new Label(composite, SWT.CENTER);
		Label_title.setText("充值");
		Label_title.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		Label_title.setBounds(46, 10, 85, 30);
		
		Label label_100 = new Label(composite, SWT.CENTER);
		
		label_100.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.NORMAL));
		label_100.setBounds(99, 116, 136, 40);
		label_100.setText("100元");
		
		Label label_200 = new Label(composite, SWT.CENTER);
		label_200.setText("200元");
		label_200.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.NORMAL));
		label_200.setBounds(369, 116, 136, 40);
		
		Label label_500 = new Label(composite, SWT.CENTER);
		label_500.setText("500元");
		label_500.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.NORMAL));
		label_500.setBounds(619, 116, 136, 40);
		
		text_money = new Text(composite, SWT.BORDER);
	
		text_money.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		text_money.setBounds(489, 265, 136, 40);
		
		Label label = new Label(composite, SWT.CENTER);
		label.setText("充值金额");
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.NORMAL));
		label.setBounds(251, 264, 136, 40);
		
		Button Bt_addmoney = new Button(composite, SWT.NONE);
		Bt_addmoney.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		Bt_addmoney.setBounds(369, 421, 122, 40);
		Bt_addmoney.setText("确认充值");
		
		Label label_3_1 = new Label(composite, SWT.CENTER);
		label_3_1.setText("元");
		label_3_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.NORMAL));
		label_3_1.setBounds(619, 265, 40, 40);
		
		//关闭按钮操作
		label_close.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.dispose();
			}
		});
		//最小化操作
		label_min.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shell.setMinimized(true);
			}
		});
		//面板拖动
		composite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				down = true;
				clickX = e.x;
				clickY = e.y;
			}
			@Override
			public void mouseUp(MouseEvent e) {
				down = false;
			}
		});
		
		composite.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				if(down) {  //down为true，说明鼠标按下，窗口跟着鼠标移动			
					shell.setLocation(shell.getLocation().x + e.x - clickX, shell.getLocation().y + e.y - clickY);
				}
				
			}
		});
		//点击100元
		label_100.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				text_money.setText("100");
			}
		});//点击200元
		label_200.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				text_money.setText("200");
			}
		});//点击500元
		label_500.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				text_money.setText("500");
			}
		});
		//点击确认充值
		Bt_addmoney.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				String account = Values.currentLoginUser.get("u_name").toString();
				String value = text_money.getText().trim();
				int money = Integer.parseInt(value);
				if(money <= 0) {
					SWTUtil.showMessage(shell, "提示", "充值无效!");
					return;
				}
				UserDao userdao = new UserDao();
				int i = userdao.updateBalance(money, account);
				if(i > 0) {
					SWTUtil.showMessage(shell, "提示", "充值成功");
					shell.dispose();
				}else {
					SWTUtil.showMessage(shell, "提示", "充值失败");
				}
			}
		});
		
	}
}
